package com.craftplay.dao;

import com.craftplay.model.Comment;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface CommentMapper {
    @Delete({
            "delete from comment",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into comment (from_id, ",
            "is_root, content, ",
            "parent_id, to_id, ",
            "is_attached, created_time, isPrivate)",
            "values (#{fromId,jdbcType=INTEGER}, ",
            "#{isRoot,jdbcType=INTEGER}, #{content,jdbcType=VARCHAR}, ",
            "#{parentId,jdbcType=BIGINT}, #{toId,jdbcType=INTEGER}, ",
            "#{isAttached,jdbcType=INTEGER}, #{createdTime,jdbcType=TIMESTAMP},",
            "#{isPrivate,jdbcType=INTEGER})"
    })
    int insert(Comment record);

    @InsertProvider(type = CommentSqlProvider.class, method = "insertSelective")
    int insertSelective(Comment record);

    @Select({
            "select",
            "id, from_id, is_root, content, parent_id, to_id, is_attached, created_time",
            "from comment",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "from_id", property = "fromId", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_root", property = "isRoot", jdbcType = JdbcType.INTEGER),
            @Result(column = "content", property = "content", jdbcType = JdbcType.VARCHAR),
            @Result(column = "parent_id", property = "parentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "to_id", property = "toId", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_attached", property = "isAttached", jdbcType = JdbcType.INTEGER),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP)
    })
    Comment selectByPrimaryKey(Long id);


    @Select({
            "select",
            "id, from_id, is_root, content, parent_id, to_id, is_attached, created_time ,isPrivate,isRead",
            "from comment",
            "where parent_id = #{parentId,jdbcType=BIGINT} and is_root = 1 order by created_time asc"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "from_id", property = "fromId", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_root", property = "isRoot", jdbcType = JdbcType.INTEGER),
            @Result(column = "content", property = "content", jdbcType = JdbcType.VARCHAR),
            @Result(column = "parent_id", property = "parentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "to_id", property = "toId", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_attached", property = "isAttached", jdbcType = JdbcType.INTEGER),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP)
    })
        //search comment by unit primary key
    List<Comment> selectByUnitId(int parentId);


    @Select({
            "select",
            "id, from_id, is_root, content, parent_id, to_id, is_attached, created_time, isPrivate ,isRead",
            "from comment",
            "where parent_id = #{parentId,jdbcType=BIGINT} order by created_time asc"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "from_id", property = "fromId", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_root", property = "isRoot", jdbcType = JdbcType.INTEGER),
            @Result(column = "content", property = "content", jdbcType = JdbcType.VARCHAR),
            @Result(column = "parent_id", property = "parentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "to_id", property = "toId", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_attached", property = "isAttached", jdbcType = JdbcType.INTEGER),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "isPrivate", property = "isPrivate", jdbcType = JdbcType.INTEGER),
            @Result(column = "isRead", property = "isRead", jdbcType = JdbcType.INTEGER)
    })

        //search comment by parent_id key
    List<Comment> selectByParentId(Long parentId);

    @UpdateProvider(type = CommentSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Comment record);

    @Update({
            "update comment",
            "set from_id = #{fromId,jdbcType=INTEGER},",
            "is_root = #{isRoot,jdbcType=INTEGER},",
            "content = #{content,jdbcType=VARCHAR},",
            "parent_id = #{parentId,jdbcType=BIGINT},",
            "to_id = #{toId,jdbcType=INTEGER},",
            "is_attached = #{isAttached,jdbcType=INTEGER},",
            "created_time = #{createdTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Comment record);

    @Update({
            "update comment",
            "set isRead = 1",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int setIsRead(Long id);


    @Select({
            "select",
            "id, from_id, is_root, content, parent_id, to_id, is_attached, created_time ,isPrivate,isRead",
            "from comment",
            "where to_id = #{to_id,jdbcType=INTEGER} and isRead = 0 "
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "from_id", property = "fromId", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_root", property = "isRoot", jdbcType = JdbcType.INTEGER),
            @Result(column = "content", property = "content", jdbcType = JdbcType.VARCHAR),
            @Result(column = "parent_id", property = "parentId", jdbcType = JdbcType.BIGINT),
            @Result(column = "to_id", property = "toId", jdbcType = JdbcType.INTEGER),
            @Result(column = "is_attached", property = "isAttached", jdbcType = JdbcType.INTEGER),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "isPrivate", property = "isPrivate", jdbcType = JdbcType.INTEGER),
            @Result(column = "isRead", property = "isRead", jdbcType = JdbcType.INTEGER)
    })
        //获取所有未读的评论
    List<Comment> getNotReadComment(int to_id);


    @Select({"<script>",
            "select",
            "*",
            "from comment",
            "where 1=1",
            "<if  test='fromId!=null'>",
            " and from_id = #{fromId}",
            "</if>",
            "<if  test='parentId!=null'>",
            " and parent_id = #{parentId}",
            "</if>",
            "</script>"
    })
    List<Comment> getCommentList(Comment comment);
}